package com.hnucm.c201901020121.Fragment.Lobby;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hnucm.c201901020121.Activity.TaskDetailAct;
import com.hnucm.c201901020121.R;
import com.hnucm.c201901020121.Utils.Bmob.BmobTask;
import com.hnucm.c201901020121.Utils.Interfaces.OnRecyclerItemClickListener;
import com.hnucm.c201901020121.Utils.Bmob.TaskShow;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;
public class PickUpTakeOutFragment extends Fragment {
    RecyclerView recyclerView;
    List<TaskShow> taskList = new ArrayList<>();
    MyAdapter myAdapter;
    private OnRecyclerItemClickListener monItemClickListener;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_pick_up_take_out, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = getActivity().findViewById(R.id.pick_up_take_out);
        myAdapter = new MyAdapter();
        LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(myAdapter);
        getTakeOrderDate();
        myAdapter.setRecyclerItemClickListener(new OnRecyclerItemClickListener() {
            @Override
            public void onItemClick(int Position, List<TaskShow> taskList) {
                Intent intent=new Intent(getContext(), TaskDetailAct.class);
                intent.putExtra("taskId",taskList.get(Position).getTaskId());
                startActivity(intent);
            }
        });
    }
    private void getTakeOrderDate() {
        String sql = "select * from BmobTask where label='取外卖' order by updatedAt desc";
        BmobQuery<BmobTask> query = new BmobQuery<BmobTask>();
        query.setSQL(sql);
        query.doSQLQuery(new SQLQueryListener<BmobTask>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void done(BmobQueryResult<BmobTask> bmobQueryResult, BmobException e) {
                if (e == null) {
                    List<BmobTask> list = (List<BmobTask>) bmobQueryResult.getResults();
                    taskList.clear();
                    for (int i = 0; i < list.size(); ++i) {
                        TaskShow taskShow = new TaskShow();
                        taskShow.setHeadimg(list.get(i).getHeadimg());
                        taskShow.setTitle(list.get(i).getTitle());
                        taskShow.setLabel(list.get(i).getLabel());
                        taskShow.setAmount(list.get(i).getAmount());
                        taskShow.setTaskId(list.get(i).getObjectId());
                        taskShow.setTaskTime(list.get(i).getTaskTime());
                        taskShow.setType(1);
                        taskShow.setTime(list.get(i).getCreatedAt().substring(5, 16));
                        taskList.add(taskShow);
                    }
                    //  Log.i("Bmob", taskList.get(0).getObjectId());
                    myAdapter.notifyDataSetChanged();//刷新列表数据
                } else {
                    Log.i("Bmob", "查找失败");
                }
            }
        });
    }
    public class MyViewHolderOne extends RecyclerView.ViewHolder {
        ImageView headimgView;
        TextView titleView, labelView, amountView, time, finishTime, taskId;
        public MyViewHolderOne(@NonNull View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.textView88);
            taskId = itemView.findViewById(R.id.textView98);
            finishTime = itemView.findViewById(R.id.textView74);
            headimgView = itemView.findViewById(R.id.circleImageView);
            titleView = itemView.findViewById(R.id.textView9);
            labelView = itemView.findViewById(R.id.textView10);
            amountView = itemView.findViewById(R.id.textView39);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(monItemClickListener!=null)
                    {
                        monItemClickListener.onItemClick(getAdapterPosition(),taskList);
                    }
                }
            });
        }
    }

    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
            monItemClickListener = listener;
        }
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_list_one, parent, false);
            return new MyViewHolderOne(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            MyViewHolderOne holder1 = (MyViewHolderOne) holder;
            holder1.titleView.setText(taskList.get(position).getTitle());
            holder1.labelView.setText(taskList.get(position).getLabel());
            holder1.amountView.setText(taskList.get(position).getAmount() + "元");
            holder1.finishTime.setText(taskList.get(position).getTaskTime() + "内完成");
            holder1.taskId.setText(taskList.get(position).getTaskId());
            holder1.time.setText(taskList.get(position).getTime());
            Glide.with(getActivity()).
                    load(taskList.get(position).getHeadimg())
                    .into(holder1.headimgView);
        }

        @Override
        public int getItemCount() {
            return taskList.size();
        }

        @Override
        public int getItemViewType(int position) {
            return taskList.get(position).getType();
        }
    }
}